<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>serialize方法使用</title>
</head>

<body>
    <form id="form">
        <input type="text" name="username" value=""><br>
        <input type="password" name="password" value=""><br>
        <input type="submit" value="提交">
    </form>
</body>
<script src='/js/jquery.min.js'></script>
<script>
    $('#form').on('submit', function() {
        // var params = $(this).serialize();
        // console.log(params);
        // console.log(this);
        var res = serializeObject($(this));
        console.log(res);
        return false;
    });

    function serializeObject(obj) {
        var params = {};
        // 返回一个数组
        var res = obj.serializeArray();
        // 将数组转换为对象
        $.each(res, function(index, value) {
            params[value.name] = value.value
        });
        // console.log(params);
        return params;
    }
</script>

</html>